package com.mysteel.order.listener;

import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * ProjectName: mail
 * ClassName: MessageListener
 * Description: 消息投递确认回调机制
 * date: 2021/03/17/017 17:15
 *
 * @author xiaochao
 */
@Slf4j
@Component
public class MessageListener implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 定制RabbitTemplate，设置自定义确认回调
     */
    @PostConstruct
    void init(){
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }

    /**
     * 消息投递到Broker后确认回调
     * @param correlationData
     * @param ack
     * @param cause
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("correlationData:{},ack:{},cause:{}",correlationData,ack,cause);
    }

    /**
     * 将消息投递到队列出错时回调
     * @param message
     * @param replyCode
     * @param replyText
     * @param exchange
     * @param routingKey
     */
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        // 修改数据库的消息记录的错误状态
        log.info("message:{},replyCode:{},replyText:{},exchange:{},routingKey:{}",message.getBody(),replyCode,replyText,exchange,routingKey);
    }
}
